package com.lwj.liwanjia.view.activity.personal.qian_bao;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.CenterBean07;
import com.lwj.liwanjia.config.URLConstant;
import com.lwj.liwanjia.utils.SpfUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by 001 on 2017/9/21.
 */

public class ZhuanZhangActivity extends BaseActivity implements View.OnClickListener {

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 0:
                    Toast.makeText(ZhuanZhangActivity.this, "数据有误", Toast.LENGTH_LONG).show();
                    break;

                case 1:
                    if (!TextUtils.isEmpty(photoUrl) && !TextUtils.isEmpty(name) && !TextUtils.isEmpty(zhangHao)) {
                        String number = et_number.getText().toString().trim();
                        Intent intent = new Intent(ZhuanZhangActivity.this, ZhuanZhangTwoActivity.class);
                        intent.putExtra("photoUrl", photoUrl);
                        intent.putExtra("name", name);
                        intent.putExtra("zhangHao", zhangHao);
                        intent.putExtra("number",number);
                        startActivity(intent);

                        finish();
                    }

                    break;
            }
        }
    };

    private TextView tv_back;
    private TextView tv_title;
    private ImageView iv_saoMiao;
    private Button bt_next;//下一步
    private EditText et_number;//手机号
    private String photoUrl;//头像地址
    private String name;//姓名
    private String zhangHao;//账号

    @Override
    protected void initView() {

        tv_back = (TextView) findViewById(R.id.tv_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        iv_saoMiao = (ImageView) findViewById(R.id.iv_right);
        bt_next = (Button) findViewById(R.id.bt_next);
        et_number = (EditText) findViewById(R.id.et_number);

        tv_back.setVisibility(View.VISIBLE);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("转账");
        iv_saoMiao.setImageResource(R.mipmap.saoyisao);
        iv_saoMiao.setVisibility(View.VISIBLE);

        tv_back.setOnClickListener(this);
        iv_saoMiao.setOnClickListener(this);
        bt_next.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_zhuan_zhang;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.tv_left:
                finish();
                break;
            //扫描
            case R.id.iv_right:
                Toast.makeText(this, "扫一扫更健康", Toast.LENGTH_SHORT).show();
                break;
            //下一步
            case R.id.bt_next:

                String number = et_number.getText().toString().trim();

                String regex = "[1][34578]\\d{9}";

                boolean flag = number.matches(regex);

                if (!TextUtils.isEmpty(number)) {

                    if (flag) {

                        //连网,判断账号
                        initOkHttp(number);
                    }else {
                        Toast.makeText(ZhuanZhangActivity.this, "请输入正确的手机号", Toast.LENGTH_LONG).show();
                    }

                } else {
                    Toast.makeText(ZhuanZhangActivity.this, "手机号不能为空", Toast.LENGTH_LONG).show();
                }


                break;
        }
    }

    //连网,判断账号
    private void initOkHttp(String number) {

        String url = URLConstant.BASE_URL + "oneCard/getTransferInfo";
        String token = SpfUtils.getSpfUtils(this).getToken();

        OkHttpClient mOkHttpClient=new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("name",number)
                .add("token",token)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall= mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //Log.d("Tag", "分类数据获取失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //Log.d("Tag", "分类图数据获取成功");

                String result = null;

                try {
                    result = response.body().string();
                    //Log.d("Tag",result);
                    parse01(result);
                } catch (Exception e) {

                }


            }
        });
    }

    //解析
    private void parse01(String result) {

        Gson gson = new Gson();
        CenterBean07 bean = gson.fromJson(result, CenterBean07.class);

        if (bean != null) {

            switch (bean.getState()) {

                case 0:
                    mHandler.sendEmptyMessage(0);
                    break;

                case 1:

                    photoUrl = bean.getData().getHead();
                    name = bean.getData().getNick();
                    zhangHao = bean.getData().getName();

                    mHandler.sendEmptyMessage(1);

                    break;
            }
        }
    }
}
